package com.example.audio_test.ui

import android.Manifest
import android.graphics.BitmapFactory
import android.graphics.ImageFormat
import android.hardware.Camera
import android.view.SurfaceHolder
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.audio_test.R
import com.example.audio_test.utils.H264Encoder
import com.kun.base.ui.AbsBaseActivity
import kotlinx.android.synthetic.main.activity_video.*
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.RuntimePermissions
import java.io.IOException

/**
 *视频录制页面
 *
 * @author : Kun
 * @date : 2021/6/21
 * @time : 16:07
 */
@RuntimePermissions
@Route(path = "/recordTest/VideoActivity")
class VideoActivity : AbsBaseActivity(), SurfaceHolder.Callback, Camera.PreviewCallback {

    private lateinit var surfaceHolder: SurfaceHolder
    private var camera: Camera? = null

    var width = 1280
    var height = 720
    var framerate = 30

    private var encoder: H264Encoder? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_video
    }

    override fun initViewModel() {
    }

    override fun initView() {
        btn_start.setOnClickListener {
            //开始录制
            startRecordWithPermissionCheck()
        }
        btn_end.setOnClickListener {
            //结束录制
            endRecord()
        }

        surfaceHolder = surface_view.holder
        surfaceHolder.addCallback(this)


    }

    override fun surfaceCreated(holder: SurfaceHolder) {

    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {

    }


    override fun onPreviewFrame(data: ByteArray?, camera: Camera?) {
        encoder!!.putData(data!!)

//        BitmapFactory.decodeByteArray(data,0,data.size)
    }

    @NeedsPermission(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun startRecord() {
        camera = Camera.open()
        camera!!.setDisplayOrientation(90)
        val parameters = camera!!.parameters
        parameters.previewFormat = ImageFormat.NV21
        parameters.setPreviewSize(width, height)
        try {
            camera!!.parameters = parameters
            camera!!.setPreviewDisplay(surfaceHolder)
            camera!!.setPreviewCallback(this)
            camera!!.startPreview()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        encoder = H264Encoder(width, height, framerate, this)
        encoder!!.startEncoder()
    }

    private fun endRecord() {
        if (camera != null) {
            camera!!.setPreviewCallback(null)
            camera!!.stopPreview()
            camera = null
        }

        encoder!!.stopEncoder()
        encoder = null
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(requestCode, grantResults)
    }
}